<?php
require_once 'init.php';
if(!$sessionID){
	header('location:login.php');
}

$action = isset($_GET['action'])?$_GET['action']:'';
$albumID = isset($_GET['albumID'])?$_GET['albumID']:0;
if(!in_array($action,array('home','wedding','studio','cooperation','guest'))){
	header('location:index.php');
}
if($action=='home'){
	$xml = simplexml_load_file($contentDB);
	$children = $xml->children();
	$content = $children[1];
	$page = $content->children();
	$photo = $page[0];
	$homeImages = $photo->children();
	$images = array();
	foreach($homeImages->children() as $img){
		$attrs = $img->attributes();
		$images[(int)$attrs['id']] = (string)$img;
	}
}else{
//elseif($action=='wedding'){
	if($albumID==0){
		header('location:album.php?action='.$action);	
	}
	$xml = simplexml_load_file($contentDB);
	$children = $xml->children();
	$content = $children[1];
	$pages = $content->children();
	if($action=='wedding'){
		$page = $pages[2];
	}elseif($action=='studio'){
		$page = $pages[3];
	}elseif($action=='cooperation'){
		$page = $pages[4];
	}elseif($action=='guest'){
		$page = $pages[5];
	}
	//$albums = $page->children();
	$images = array();
	foreach($page->children() as $album){
		$attrs = $album->attributes();
		if($albumID==$attrs['id']){
			foreach($album->children() as $item){
				$attr = $item->attributes();
				$images[(int)$attr['id']] = (string)$item;	
			}
		}
	}
}
?>
<html>
<head>
	<meta charset="utf-8" />
	<link rel="stylesheet" href="css/default.css" />
</head>
<body>
	<div class="header"></div>
	<div class="middle">
		<div class="left">
			<?php require_once 'left.php';?>		
		</div>
		<div class="right">
			<?php if($action=='home'):?>
			<a href="imgAdd.php?action=home" >添加图片</a>
			<?php else:?>
			<a href="imgAdd.php?action=<?php echo $action;?>&albumID=<?php echo $albumID;?>" >添加图片</a>
			<?php endif;?>
			<table class="imageTable">
				<tr>
					<th>图片</th><th>操作</th>
				</tr>
			<?php foreach($images as $key=>$val):?>
				<tr>
					<td><img src="<?php echo $host.'/'.$val;?>" width="100" height="100" /></td>
					<td>
						<?php if($action=='home'):?>
						<a href="imgEdit.php?action=<?php echo $action;?>&id=<?php echo $key;?>">编辑</a> | 
						<a href="imgDel.php?action=<?php echo $action;?>&id<?php echo $key;?>">删除</a>
						<?php else:?>
						<a href="imgEdit.php?action=<?php echo $action;?>&id=<?php echo $key;?>&albumID=<?php echo $albumID;?>">编辑</a> | 
						<a href="imgDel.php?action=<?php echo $action;?>&id<?php echo $key;?>&albumID=<?php echo $albumID;?>">删除</a>
						<?php endif;?>
					</td>
				</tr>
			<?php endforeach;?>
			</table>
	</div>
	</div>
	<div class="footer">
	</div>
</body>
</html>
